package com.foreveross.fetl.queue.util;

import java.util.Date;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.fetl.application.ILogMessageApplication;
import com.foreveross.fetl.vo.LogMessageVO;

public class LogMessageUtils {

	
	public static void build(String excuteName,String excuteClass,int type, String result, int failuresNumber, int tasksNumber, int insertNumber, String taskTime,
			long excuteStartTime,String rowKey){
		LogMessageVO vo=new LogMessageVO();
		vo.setExecuteClass(excuteClass);
		vo.setExecuteName(excuteName);
		vo.setExecuteStartTime(new Date(excuteStartTime));
		vo.setExecuteType(type);
		vo.setResult(result);
		vo.setExecuteEndTime(new Date());
		vo.setFailuresNumber(failuresNumber);
		vo.setTasksNumber(tasksNumber);
		vo.setInsertNumber(insertNumber);
		vo.setTaskTime(taskTime);
		vo.setExecuteTotalTime(System.currentTimeMillis()-excuteStartTime);
		vo.setRowKey(rowKey);
		saveOrUpdateLog(vo);
	}
	
	public static void build(String excuteName,String excuteClass,int type, String result, 
			int failuresNumber, int tasksNumber, int insertNumber, String taskTime,
			long excuteStartTime){
			build(excuteName, excuteClass, type, result, failuresNumber,
					tasksNumber, insertNumber, taskTime, excuteStartTime, null);
	}
	
	/**
	 * 更新log 状态和信息
	 * @param vo
	 */
	public static void saveOrUpdateLog(LogMessageVO vo) {
		ILogMessageApplication logMessageApplication=InstanceFactory.getInstance(ILogMessageApplication.class);
		LogMessageVO logMes = null;
		if (null != vo) {
			logMes = logMessageApplication.getOneLogMessage(
					vo.getExecuteClass(), vo.getExecuteName(),
					vo.getExecuteStartTime(), 1);
		}else{
			return ;
		}
		if (null != logMes.getId() && 0 != logMes.getId()) {
			vo.setId(logMes.getId());
			logMessageApplication.update(vo);
		} else {
			logMessageApplication.create(vo);
		}
	}
	
	/**
	 * 创建log日志
	 * @param executeName 
	 * @param executeClass 
	 */
	public static void create(String executeName, String executeClass){
		LogMessageVO startLogMessage =new LogMessageVO();
		startLogMessage.setExecuteClass(executeClass);
		startLogMessage.setExecuteName(executeName);
		startLogMessage.setExecuteStartTime(new Date());
		startLogMessage.setExecuteType(1);
		ILogMessageApplication logMessageApplication=InstanceFactory.getInstance(ILogMessageApplication.class);
		logMessageApplication.create(startLogMessage);
	}
	

}
